<?php

namespace App\Http\Controllers\Api;

use App\Util\RandomHelper;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Mrgoon\AliSms\AliSms;

class SmsController extends Controller
{
    public function sendRegister(Request $request){
        $ip = $request->getClientIp();
        //ip白名单，不需要图形验证码
        $whiteList = [
           '220.249.7.174'
        ];

        if(!in_array($ip,$whiteList)){
            $captcha = $request->input('captcha');
            if( !captcha_check($captcha) ){
                $return = array(
                    'success' => false,
                    'msg' => "图形验证码不正确！"
                );
                return $return;
            }
        }

        $phone = $request->get('phone');
        $alidayu = app('alidayu');//get the instance of alidayu
        //生成短信验证码，并持有
        Cache::put($phone,RandomHelper::randomNumber(), 10);
        $alidayu->setSmsParam(['code'=>Cache::get($phone),'product'=>'有品位旅游年卡']);//set the params
        $alidayu->setSmsTemplateCode('SMS_15405135');
        $res = $alidayu->sendCheckCodeSms($phone);//send sms
        if ($res) {
            $return = array(
                'success' => true,
                'msg' => "发送成功！"
            );
        } else {
            //失败尝试2次  简单写法
            $res = $alidayu->sendCheckCodeSms($phone);//send sms
            if(!$res){
                $res = $alidayu->sendCheckCodeSms($phone);//send sms
                if($res){
                    $return = array(
                        'success' => true,
                        'msg' => "发送成功！"
                    );
                }
                else{
                    $return = array(
                        'success' => false,
                        'msg' => "发送失败！"
                    );
                }
            }
        }
        return $return;
    }

    public function sendWapRegister(Request $request){
        $phone = $request->get('phone');
//        $captcha = $request->input('captcha');
//        if( !captcha_check($captcha) ){
//            $return = array(
//                'success' => false,
//                'msg' => "图形验证码不正确！"
//            );
//            return $return;
//        }
        //生成短信验证码，并持有
        Cache::put($phone,RandomHelper::randomNumber(), 10);
        $aliSms = new AliSms();
        $result = $aliSms->sendSms($phone, 'SMS_89565022', ['code'=> Cache::get($phone)]);
        if ($result) {
            $return = array(
                'success' => true,
                'msg' => "发送成功！"
            );
        } else {
            $return = array(
                'success' => false,
                'msg' => "发送失败！"
            );
        }
        return $return;
    }

    /**
     * 银联支付
     * czs
     */
    public function pay() {
        $gateway    = Omnipay::create('UnionPay_Express');
        $config = config('laravel-omnipay.options');
        $gateway->setMerId($config['merId']);
        $gateway->setReturnUrl($config['returnUrl']);
        $gateway->setNotifyUrl($config['notifyUrl']);

        $order = [
            'orderId'   => date('YmdHis'), //Your order ID
            'txnTime'   => date('YmdHis'), //Should be format 'YmdHis'
            'orderDesc' => 'My order title', //Order Title
            'txnAmt'    => '100', //Order Total Fee
        ];

        $response = $gateway->purchase($order)->send();

        $response->getRedirectHtml(); //For PC/Wap
    }
}
